package demo.hms.RoomType.service.servicelmpl;

import demo.hms.RoomType.dao.RoomTypeDao;
import demo.hms.RoomType.service.RoomTypeService;
import demo.hms.mybatis.entity.RoomType;
import demo.hms.util.Result;
import demo.hms.util.ResultSubmit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED,rollbackFor = Exception.class)
public class RoomTypeServicelmpl implements RoomTypeService {
    @Resource
    private RoomTypeDao dao;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
    @Override
    public Result selectAll(String roomTypeName) {
        return ResultSubmit.success(dao.selectAll("", roomTypeName));
    }
    @Override
    public Result insert(RoomType roomType) {
        roomType.setRoomTypeId(Math.abs((int) System.currentTimeMillis()));
        roomType.setDataUpdateTime(format.format(new Date()));
        roomType.setDeleteIdentity("未删除");
        // todo 系统记录操作
            if (dao.insert(roomType) > 0) {return ResultSubmit.success();}
        // 手动回滚
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return ResultSubmit.fail();
    }
    @Override
    public Result update(RoomType roomType) {
        roomType.setDataUpdateTime(format.format(new Date()));
        roomType.setDeleteIdentity("未删除");
        int i = dao.update(roomType);
        int j = dao.insert(roomType);
        System.out.println("房间类型操作: 删除数量" + i + "条, 新增数量" + j + "条");
        // todo 系统记录操作
        if (i > 0 || j > 0) {return ResultSubmit.success();}
        // 手动回滚
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return ResultSubmit.fail();}
    @Override
    public Result delete(String roomTypeId) {
        if (dao.update( dao.selectAll(roomTypeId,"").get(0)) > 0) {return ResultSubmit.success();}
        // 手动回滚
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return ResultSubmit.fail();
    }
}
